<?php
define( TMP_HARI_KERJA,'temp_tmn_hari_new');
define( TMP_OVERTIME_KERJA,'temp_tmn_overtime_new');
define( TMP_KJK,'temp_tmn_kjk_new');
define( TMP_KHK,'temp_tmn_khk_new');

///////////////////////START KJK
function proses_kjk(){
	my_query("TRUNCATE TABLE  ". TMP_KJK);
	$end=get_batas_tmt() ;
	$query = " SELECT a.karyawan_id,a.nik FROM karyawan a
				INNER JOIN kekaryawanan b ON b.karyawan_id = a.karyawan_id 
				WHERE b.tmb  <= '{$end}' 
				AND a.karyawan_id NOT IN (select karyawan_id FROM karyawan_disable)
				AND a.karyawan_id NOT IN (select karyawan_id FROM pemberhentian  WHERE tmt <= '{$end}') 	
			";	 
	$result = my_query($query);
	$total_records = my_num_rows($result ); 
	if(!($total_records > 0)){
		return 0;
	}
	while($ey = my_fetch_array($result)){ 
		$harikerjalebih = total_kjk($ey['karyawan_id']);
		$kekaryawanan = my_get_data_by_id( 'kekaryawanan' , 'karyawan_id' , $ey['karyawan_id']);
		if($harikerjalebih == 0)continue;
		$value = $harikerjalebih ;
		$tunjangan = KONSTANTA_TUNJANGAN_KJK * $value * $kekaryawanan['gapok_km'] / 173;
		$datas = array(
			'karyawan_id'=>my_type_data_str($ey['karyawan_id']),
			'nik'=>my_type_data_str(sprintf('%07d',$nik)),
			'njam'=>my_type_data_int($value),  
		);
		my_insert_record( TMP_KJK ,$datas);
		 
	}
	return true;
}



function page_kalkulasi(){
	my_set_code_js('
	$(document).ready(function() {
       	$(\'#onebar\').load(\'index.php?com='.$_GET['com'].'&task=kalkulasi_waktu_kerja&'.rand(0,9).'='.rand(0,99999).'\'); 
	});
	');
	return '<div id="onebar" style="width:100%;text-align:center;"><img src="components/system/jquery/facebox/loading.gif" /><br/>On processing!!!</div>';
}
  
function total_kjk($karyawan_id  ){
	$query = "SELECT COUNT(*) AS ttl FROM ". TMP_HARI_KERJA ." WHERE karyawan_id = {$karyawan_id} AND  
		( status_absen = 'N' OR 
			status_absen = '1' OR
				status_absen = '2' OR
					status_absen = '3' )"; 
	$result = my_query($query);
	$row = my_fetch_array($result);
	$hari =  $row['ttl'] - (int)$_POST['waktu_kerja'];
	$hari = ( $hari>0) ? $hari : 0 ;
	return 8 * $hari;
} 
///////////////////////END KJK

//START KHK

function proses_khk(){
	my_query("TRUNCATE TABLE ".TMP_KHK);
	$end=get_batas_tmt() ;
	$query = " SELECT a.karyawan_id,a.nik FROM karyawan a
				INNER JOIN kekaryawanan b ON b.karyawan_id = a.karyawan_id 
				WHERE b.tmb  <= '{$end}' 
				AND a.karyawan_id NOT IN (select karyawan_id FROM karyawan_disable)
				AND a.karyawan_id NOT IN (select karyawan_id FROM pemberhentian  WHERE tmt <= '{$end}') 	
			";	 
	$result = my_query($query);
	$total_records = my_num_rows($result ); 
	if(!($total_records > 0)){
		return 0;
	}
	while($ey = my_fetch_array($result)){ 
		$harikerjalebih = total_khk($ey['karyawan_id']); 
		$jhit = $harikerjalebih * 1.5 ; 
		if($jhit == 0)continue;
	 
		$datas = array(
			'nik'	=> my_type_data_str($ey['nik']),
			'karyawan_id'	=> my_type_data_str($ey['karyawan_id']),
			'nhari'	=> my_type_data_str($jhit), 
		);
		my_insert_record( TMP_KHK , $datas); 
		 
	}
	return true;
}


  
function total_khk($karyawan_id  ){
	$query = "SELECT COUNT(*) AS ttl FROM ".TMP_KHK." WHERE karyawan_id = {$karyawan_id} "; 
		$query .= " AND 
		(  
			status_absen = '1' OR 
			status_absen = '3' 
		)"; 
	$result = my_query($query);
	$row = my_fetch_array($result);
	return $row['ttl'];
} 

//END KHK

function list_realisasi_karyawan_by_periode_id(){
	$periode_id =get_periode_absen();
	$periode = my_get_data_by_id('pr_periode','periode_id',$periode_id);
	$bulan = (int) date('m' , strtotime($periode['tanggal']));
	$tahun = (int) date('Y' , strtotime($periode['tanggal']));
	$end=$periode['tahun'].'-'. sprintf( '%02d',$bulan) .'-10' ;
	$header1 = array(
		'#'=>array('style'=>'border-bottom:2px solid;width:3%'),
		'nik'=>array('style'=>'border-bottom:2px solid;width:8%'),
		'nama'=>array('style'=>'border-bottom:2px solid;width:20%'),
	//	'WK'=>array('style'=>'border-bottom:2px solid;width:3%'),
	//	'KEL'=>array('style'=>'border-bottom:2px solid;width:3%'),
		  
	);
	$header2= array();
	$tanggals = list_kalender($periode['dari'] , $periode['sampai']  );
	$tanggals = array_unique($tanggals) ;
	foreach($tanggals as $tgl){ 
		$header2[date('d',strtotime($tgl) )] = array('style'=>'border-bottom:2px solid;width:1%');
	} 
	$header = $header1 + $header2  ;
	if(isset($_GET['key'])){
		$query = "
			SELECT * FROM karyawan a 
			INNER JOIN biodata b 
			ON a.biodata_id = b.biodata_id
			WHERE a.karyawan_id NOT IN (select karyawan_id FROM karyawan_disable)	
			AND a.karyawan_id NOT IN (select karyawan_id FROM pemberhentian  WHERE TMT <= '{$end}') 	
			AND ( b.nama LIKE '%{$_GET['key']}%' OR a.nik='{$_GET['key']}')
			ORDER BY a.nik
		";
	}else{
		$query = "
			SELECT * FROM karyawan a 
			INNER JOIN biodata b 
			ON a.biodata_id = b.biodata_id
			WHERE a.karyawan_id NOT IN (select karyawan_id FROM karyawan_disable)	
			AND a.karyawan_id NOT IN (select karyawan_id FROM pemberhentian  WHERE TMT <= '{$end}') 	
			ORDER BY a.nik
		";
	}
	$result = my_query($query);
	$total_records = my_num_rows($result );
	$scroll_page =5;   
	$per_page =24;  
	$current_page = isset($_GET['page']) ? (int) $_GET['page'] : 1 ; 
	if($current_page < 1){
		$current_page = 1;
	}		 
	$pager_url  ="index.php?com=jadwal_wk_karyawan&task={$_GET['task']}&key={$_GET['key']}&page=";	 
	$inactive_page_tag = 'style="padding:4px;background-color:#BBBBBB"';  
	$previous_page_text = ' Mundur '; 
	$next_page_text = ' Maju ';  
	$first_page_text = ' Awal '; 
	$last_page_text = ' Akhir ';
	
	$kgPagerOBJ = new kgPager();
	$kgPagerOBJ->pager_set(
		$pager_url, 
		$total_records, 
		$scroll_page, 
		$per_page, 
		$current_page, 
		$inactive_page_tag, 
		$previous_page_text, 
		$next_page_text, 
		$first_page_text, 
		$last_page_text ,$pager_url_last); 
	 		
	$result = my_query($query ." LIMIT ".$kgPagerOBJ->start.", ".$kgPagerOBJ->per_page); 	
	$row = array(); 
	$i = ($current_page  - 1 ) * 24; 
	while($ey = my_fetch_array($result )){
		$i++;
		$karyawan =  loaddata_karyawan($ey['karyawan_id']);  
		
		$row1 = array(
			'#'=>position_text_align ($i, 'center'), 
			'nik'=>position_text_align( $karyawan['nik'], 'center'),
			'nama'=>substr($karyawan['nama_gelar'], 0,12), 
		//	'wk'=>position_text_align( $karyawan['waktu_kerja'] , 'center'), 
		//	'kel'=>position_text_align( $karyawan['kelompok'] , 'center'), 
			 
			 
		);
		$row2  = array(); 
		foreach($tanggals as $tgl){ 
			$dt = get_realiasi_kalkulasi($ey['karyawan_id'] , $tgl);
			$row2[date('d',strtotime($tgl) )] =  position_text_align( $dt   , 'right');
		} 
		$row[] = array_merge( $row1 , $row2  );
	}
	
	$datas = table_rows($row); 
	$paging = $kgPagerOBJ ->showPaging();
	$view =  form_header( "detail pegawai" , "cp"  );
	$view .= form_field_display( '<span class="label_form">'.  date('d-m-Y' ,strtotime($periode['dari'])) .' &nbsp; s/d &nbsp; '.date('d-m-Y' ,strtotime($periode['sampai'])).'</span>'   , "<b>Periode Absen</b>"    ); 
	$view .= form_footer( );	
	$search = '<form method="GET"><input type="hidden" name="com" value="'.$_GET['com'].'" /><span style="font:10px verdana"><b>Pencarian cepat</b></span><br/><input type="text"  style="width:200px;" name="key" value="'.(isset($_GET['key']) ? $_GET['key']: "").'" /><input class="simple_search" type="submit" value=" CARI " /> </form>';
	$menuontop = array(  
			'Resume' => array('onclick'=>'javascript:location.href=\'index.php?com='.$_GET['com'].'\''),
		);
	$printmenu = additional_menu_on_list( $menuontop ,$search);
	return  $view. $printmenu .table_builder($header , $datas ,  15 ,false ,$paging );
}

function get_realiasi_kalkulasi($karyawan_id , $date ){
	list($year , $month, $day ) = explode("-" , $date);
	$day = (int) $day;
	$query = " SELECT status_absen FROM ".TMP_HARI_KERJA." 
			WHERE tgl_index = '{$day }' AND karyawan_id = {$karyawan_id}";
	$result = my_query($query);   
	$re = my_fetch_array($result);
	return $re['status_absen'];
}

function trap_data_waktu_kerja(){
	my_query("TRUNCATE TABLE  ". TMP_HARI_KERJA);
	my_query("TRUNCATE TABLE  ". TMP_OVERTIME_KERJA);
	my_query("TRUNCATE TABLE  temp_proses_finger" );
	$end=get_batas_tmt() ;
	
	$periode_id =get_periode_absen();
	$periode = my_get_data_by_id('pr_periode','periode_id',$periode_id);
	$startdate = $periode['dari'];	
	$enddate  = $periode['sampai'];
	
	$dates = list_kalender($startdate , $enddate  );
	
	$query = " SELECT a.karyawan_id FROM karyawan a
				INNER JOIN kekaryawanan b ON b.karyawan_id = a.karyawan_id 
				WHERE b.tmb  <= '{$end}' 
				AND a.karyawan_id NOT IN (select karyawan_id FROM karyawan_disable)
				AND a.karyawan_id NOT IN (select karyawan_id FROM pemberhentian  WHERE tmt <= '{$end}') 	
			";	 
	$result = my_query($query);
	$i=0;
	while($ey = my_fetch_array($result)){
		$karyawan = loaddata_karyawan( $ey['karyawan_id'] ); 
		if((int) $karyawan['kelompok_shift_id'] <= 1 ){
			
			foreach($dates as $date){
				set_finger_result($date , $ey['karyawan_id'] );
				//CHECK PERJALAN DINAS
				list($year , $month , $day) = explode( "-" , $date);
				$status_absen =  is_hari_libur( $date ) ? 'X' : false;
				
				if(! $status_absen)
					$status_absen = check_perjalanan_dinas( $ey['karyawan_id'] ,$date );
				
				if(! $status_absen)
					$status_absen = check_ijin( $ey['karyawan_id'] ,$date );
					
				if(! $status_absen)
					$status_absen = check_cuti( $ey['karyawan_id'] ,$date );
					
				if(! $status_absen)
					$status_absen = 'N';
				
				entry_data_per_day($ey['karyawan_id'] , $libur, $day , $status_absen); 
			}
			
		}else{
			foreach($dates as $date){
				set_finger_result($date , $ey['karyawan_id'] );
				list($year , $month , $day) = explode( "-" , $date);
				$status_absen_schedule = get_jadwal_kelompok($ey['karyawan_id'] ,$day);
					//get_kelompok_shift_jadwal_kalkulasi($day , (int) $karyawan['kelompok_shift_id']  ,$periode_id);
				$status_absen =  ($status_absen_schedule <> 'X') ? false : 'X';
				if(! $status_absen)
					$status_absen = check_perjalanan_dinas( $ey['karyawan_id'] ,$date );
				
				if(! $status_absen)
					$status_absen = check_ijin( $ey['karyawan_id'] ,$date );
					
				if(! $status_absen)
					$status_absen = check_cuti( $ey['karyawan_id'] ,$date );

				if(! $status_absen)
					$status_absen = $status_absen_schedule; 
					
				entry_data_per_day( $ey['karyawan_id'], $libur, $day , $status_absen );
			}
		} 
				
		
		
		kalkulasi_lembur($ey['karyawan_id'], $startdate , $enddate);
		$i++; 
	}
}

function get_jadwal_kelompok($karyawan_id,$day){
	$query = "SELECT * FROM a_temp_jadwal_karyawan WHERE karyawan_id = {$karyawan_id}";
	$result = my_query($query);
	$row = my_fetch_array($result);
	return $row['h'.$day];
}
 
function get_kelompok_shift_jadwal_kalkulasi($day , $kelompok_shift_id ,$periode_id){

	$query = "SELECT tgl_{$day} FROM a_kalendar_shift 
		WHERE periode_id = {$periode_id} 
		AND kelompok_shift_id={$kelompok_shift_id}";
	$result = my_query($query);   
	$re = my_fetch_array($result);
	return $re['tgl_'.$day];
}

function entry_data_per_day($karyawan_id , $libur, $day , $status_absen){
	$datas = array(
		'tgl_index'		=>  my_type_data_str($day),
		'hari_libur'	=>	my_type_data_str($libur),
		'karyawan_id'	=>  my_type_data_int($karyawan_id),
		'status_absen'	=>  my_type_data_str($status_absen) 
	);
	return my_insert_record(TMP_HARI_KERJA , $datas);
}



function check_perjalanan_dinas($karyawan_id,$date ){

	$query = "	SELECT * FROM spd_simple 
				WHERE karyawan_id = {$karyawan_id} AND 
				(
						DATE( tanggal_mulai ) <= DATE(  '{$date}' ) 
					AND DATE( tanggal_akhir ) >= DATE(  '{$date}' )
				) 
			";
	$result = my_query($query);
	if( my_num_rows($result) > 0 ){
		$row = my_fetch_array($result);
		if($row['antar_pasien'] =='Yes')
			return false;
		else
			return "BT";
	}
	
	return false;
}

function check_cuti($karyawan_id , $date){
	$query = "	SELECT * FROM surat_cuti WHERE karyawan_id ={$karyawan_id} AND
				(
						DATE( tanggal_mulai ) <= DATE(  '{$date}' ) 
					AND DATE( tanggal_selesai ) >= DATE(  '{$date}' )
				) ";	 
	$result = my_query($query);
	if( my_num_rows($result) > 0 ){
		$row = my_fetch_array($result);
		if( $row['kelompok_cuti_id'] == '6' ){
			return "BL";
		}else
		if($row['kelompok_cuti_id'] == '7'){
			return "C";
		}else
		if($row['kelompok_cuti_id'] == '8'){
			return "BP";
		}
		return false;
	}
	
	return false;
}

function check_ijin($karyawan_id , $date){
	$query = "	SELECT * FROM a_surat_ijin 
				WHERE karyawan_id ={$karyawan_id} 
				AND	( DATE( tanggal ) = DATE('{$date}') ) 
				AND full_day = 'Y' ";
	$result = my_query($query);
	if( my_num_rows($result) > 0 ){
		$row = my_fetch_array($result);
		if( $row['full_day'] == 'Y' ){
			if($row['potong_insentif'] == 'T')
				return "PP";
			else
				return "UP";
				
		} 
		return false;
	}
	
	return false;
}

function is_hari_libur( $date ){
	$dayname = date( 'D' , strtotime($date));  
	$freedays = array( 'Sat' , 'Sun' );
	if( in_array($dayname , $freedays ) ){
		
		return true;
	}
	$query = "SELECT * FROM a_hari_libur WHERE DATE(tanggal) = DATE('{$date}') ";
	$result = my_query($query);
	if( my_num_rows($result) > 0 ){
		return true;
	}
	
	return false;
}

function kalkulasi_lembur($karyawan_id, $datestart , $dateend){
	$query = "SELECT  * ,TIME_TO_SEC( TIMEDIFF(  sampai_jam ,dari_jam) ) /3600 AS durasi
			FROM a_spl_det  WHERE karyawan_id = {$karyawan_id} AND (DATE(tanggal) >= '{$datestart}' AND DATE(tanggal) <='{$dateend}' )";
	$result = my_query($query);
	$hari = array(
		'HL'=>'O',
		'HB'=>'H',
		'HR'=>'R',
	);
	$jam = 0;
	$hourpay = 0;
	$baris = 0;
	while( $row = my_fetch_array($result) ){
		$karyawan = loaddata_karyawan($row['karyawan_id']);
		if($row['durasi']  <= 0) $row['durasi'] = $row['jumlah_jam'];
		
		if($row['jenis_hari'] == 'HB'){
			$hourpay = 2 * $row['durasi'] - 0.5;
		}elseif($row['jenis_hari'] == 'HL'){
			$hourpay = 2 * $row['durasi'];
		}elseif($row['jenis_hari'] == 'HR'){
			$hourpay = 3* $row['durasi'];
		}else{
			$hourpay =  $row['durasi'];
		}
		
		$jam += $hourpay;
		$hit = ($jam > 56) ? 'Y' :'N' ;
		
		$nominal = $karyawan['gapok_km'] * $hourpay * ( 1 / 173 );
		
		$check = check_exists_on_date($row['karyawan_id'] , $row['tanggal'] , $baris , $datestart ,$dateend); 
		$groupday = $check ? sprintf('%02d',$check): '';
		$datas = array(
			'karyawan_id'	=> my_type_data_int($row['karyawan_id']),	
			'nik'			=> my_type_data_str($karyawan['nik']),	
			'tanggal'		=> my_type_data_str($row['tanggal']),
			'daytype'		=> my_type_data_str($hari[$row['jenis_hari']]),
			'groupday'		=> my_type_data_str($groupday),
			'hour'			=> my_type_data_str($row['durasi']),
			'hourpay'		=> my_type_data_str($hourpay ),
			'out_of_count'	=> my_type_data_str($hit),
			'nominal'		=> my_type_data_str( $nominal ),
		);
		my_insert_record( TMP_OVERTIME_KERJA, $datas );
	}
	return true;
	
}

function kalkulasi_lembur_old($karyawan_id ,  $datestart , $dateend){
	$query = "SELECT TIME_TO_SEC( TIMEDIFF(  sampai_jam ,dari_jam) ) /3600 AS durasi ,karyawan_id,tanggal 
	FROM a_spl  WHERE DATE(tanggal) >= '{$datestart}' AND DATE(tanggal) <='{$dateend}' ";
	$result = my_query($query);
	$jam = 0;
	$hourpay = 0;
	$baris = 0;
	while( $row = my_fetch_array($result) ){
		$baris++;
		$karyawan = loaddata_karyawan($karyawan_id);
		$day_type = is_hari_libur( $date ) ? 'O' : 'H' ;
		
		if($day_type == 'O'){
			$hourpay = 2 * $row['durasi'] - 0.5;
		}else{
			$hourpay = 2 * $row['durasi'];
		}
		$jam += $hourpay;
		$hit = ($jam > 56) ? 'Y' :'N' ;
		
		$check = check_exists_on_date($row['karyawan_id'] , $row['tanggal'] , $baris , $datestart ,$dateend); 
		$groupday = $check ? sprintf('%02d',$check): ''; 
		$datas = array(
			'karyawan_id'	=> my_type_data_int($row['karyawan_id']),	
			'nik'			=> my_type_data_str($karyawan['nik']),	
			'tanggal'		=> my_type_data_str($row['tanggal']),
			'daytype'		=>	 my_type_data_str($day_type),
			'groupday'		=> my_type_data_str($groupday),
			'hour'			=> my_type_data_str($row['durasi']),
			'hourpay'		=> my_type_data_str($hourpay ),
			'out_of_count'	=> my_type_data_str($hit),
			'nominal'		=> my_type_data_str(0),
		);
	}
	return true;
}


function check_exists_on_date(	$karyawan_id , $tanggal, $baris , $datestart ,$dateend){
	$query = "SELECT karyawan_id FROM a_spl WHERE DATE(tanggal)  = '{$$tanggal}' ";
	$result = my_query($query);
	$baris_query = my_num_rows($result);
	$baris = $baris - 1;
	if($baris_query > $baris){
		return $baris++;
	}
	return false;
}

function check_realisasi($karyawan_id , $date){
	$query = "	SELECT * FROM a_realisasi 
				WHERE karyawan_id ={$karyawan_id} 
				AND	( DATE( tanggal ) = DATE('{$date}') )   ";
	$result = my_query($query);
	if( my_num_rows($result) > 0 ){
		return true;
	}
	
	return false;
	
}


function list_karyawan_hari_kerja_kalkulasi(  ){
	my_set_code_js('
		function confirmRst(){
			var t = confirm(\'Yakin akan reset data ?\');
			if(t){
				location.href=\'index.php?com='.$_GET['com'].'&task=rst\';
			}
			return false;
		}
	');
	$header = array(
		'NIK'=>array('style'=>'text-align:center;border-bottom:2px solid;width:10%'),  
		'Nama'=>array('style'=>'text-align:left;border-bottom:2px solid;width:27%'),  
		'Jabatan'=>array('style'=>'text-align:left;border-bottom:2px solid;width:36%'),  
		'IN'=>array('style'=>'text-align:center;border-bottom:2px solid;width: 4%'),  
		'PP'=>array('style'=>'text-align:center;border-bottom:2px solid;width:4%'), 
		'BT'=>array('style'=>'text-align:center;border-bottom:2px solid;width:4%'), 
		'BL'=>array('style'=>'text-align:center;border-bottom:2px solid;width:4%'), 
		'BP'=>array('style'=>'text-align:center;border-bottom:2px solid;width:4%'), 
		'SD'=>array('style'=>'text-align:center;border-bottom:2px solid;width:4%'), 
		'C'=>array('style'=>'text-align:center;border-bottom:2px solid;width:4%'), 
	);
	$row = array(); 
	$paging ='';
	
	$end=get_batas_tmt() ;
	if(isset($_GET['key'])){
		$query = "
			SELECT a.karyawan_id FROM karyawan a 
			INNER JOIN kekaryawanan b ON b.karyawan_id = a.karyawan_id 
			INNER JOIN biodata c ON a.biodata_id = c.biodata_id 
			WHERE b.tmb  <= '{$end}' 
			AND a.karyawan_id NOT IN (select karyawan_id FROM karyawan_disable)
			AND a.karyawan_id NOT IN (select karyawan_id FROM pemberhentian  WHERE tmt <= '{$end}') 	
			AND c.nama LIKE '%{$_GET['key']}%' OR a.nik ='{$_GET['key']}'
			ORDER BY a.nik
		";
	}else{	
		$query = " SELECT a.karyawan_id FROM karyawan a
				INNER JOIN kekaryawanan b ON b.karyawan_id = a.karyawan_id 
				WHERE b.tmb  <= '{$end}' 
				AND a.karyawan_id NOT IN (select karyawan_id FROM karyawan_disable)
				AND a.karyawan_id NOT IN (select karyawan_id FROM pemberhentian  WHERE tmt <= '{$end}')
				ORDER BY a.nik				
			";	
	}
	$result = my_query($query);
	$total_records = my_num_rows($result );
	$scroll_page =7;  
	$per_page =24;  
	$current_page = isset($_GET['page']) ? (int) $_GET['page'] : 1 ; 
	if($current_page < 1){
		$current_page = 1;
	}	 
	$pager_url  ="index.php?com={$_GET['com']}&task={$_GET['task']}&periode_id={$_GET['periode_id']}&field={$_GET['field']}&key={$_GET['key']}&page=";	 
	$inactive_page_tag = 'style="padding:4px;background-color:#BBBBBB"';  
	$previous_page_text = ' Mundur '; 
	$next_page_text = ' Maju ';  
	$first_page_text = ' Awal '; 
	$last_page_text = ' Akhir ';
	
	
	$kgPagerOBJ = new kgPager();
			$kgPagerOBJ->pager_set(
				$pager_url, 
				$total_records, 
				$scroll_page, 
				$per_page, 
				$current_page, 
				$inactive_page_tag, 
				$previous_page_text, 
				$next_page_text, 
				$first_page_text, 
				$last_page_text ,$pager_url_last); 
				
	$result = my_query($query ." LIMIT ".$kgPagerOBJ->start.", ".$kgPagerOBJ->per_page); 	
	
	$i = ($current_page  - 1 ) * 25; 
	
		while($ey = my_fetch_array($result)){
			 
			$karyawan = loaddata_karyawan($ey['karyawan_id']);
			if(empty($karyawan['gelar'])){
				$nama_karyawan=ucfirst($karyawan['nama']) ;
			}else{
				$nama_karyawan=trim(ucfirst($karyawan['nama'])).", ".$karyawan['gelar'] ;
			}

			$jabatan = my_get_data_by_id('jabatan' ,'jabatan_id' , ($karyawan['jabatan_id']<>'' ? $karyawan['jabatan_id'] : 0 ));
			
			 $row[] = array(
				'nik'=>position_text_align ($karyawan['nik'] ,'center'),    
				'gd'=> $nama_karyawan ,      
				'jabatan'=> $jabatan['nama_jabatan']   ,
				'in'=>position_text_align(  total_masuk($ey['karyawan_id']  )  ,'center'),
				'pp'=> position_text_align( total_masuk($ey['karyawan_id'] , 'PP' )  ,'center'),
				'bt'=> position_text_align( total_masuk($ey['karyawan_id'] , 'BT' )  ,'center'),
				'bl'=>position_text_align(  total_masuk($ey['karyawan_id'] , 'BL')   ,'center'),
				'bp'=> position_text_align( total_masuk($ey['karyawan_id'] , 'BP')   ,'center'),
				'sd'=> position_text_align( total_masuk($ey['karyawan_id'] , 'SD')  ,'center'),
				'c'=> position_text_align( total_masuk($ey['karyawan_id'] , 'C')   ,'center')
			);
			
		}
	$paging = $kgPagerOBJ ->showPaging();	
	//} 
	$datas = table_rows($row);
	
$periode_id =get_periode_absen();
	$periode = my_get_data_by_id('pr_periode','periode_id',$periode_id); 
	$search = '<form method="GET"><input type="hidden" name="com" value="'.$_GET['com'].'" />
	<span style="font:10px verdana"><b>Pencarian cepat</b></span>  
	<br/><input type="text"  style="width:200px;" name="key" value="'.(isset($_GET['key']) ? $_GET['key']: "").'" />
	<input class="simple_search" type="submit" value=" CARI " /> </form>';

	$view ='<table width="90%" cellspacing="0" cellpadding="4"><tr>'; 
	$view .='<table width="100%"   style="border-collapse:collapse;border-color:white" cellspacing="0" cellpadding="2">';
	$view .= form_field_display( '<span class="label_form">'.  date('d-m-Y' ,strtotime($periode['dari'])) .' &nbsp; s/d &nbsp; '.date('d-m-Y' ,strtotime($periode['sampai'])).'</span>'   , "<b>Periode Absen</b>"    ); 
	
	//$button_import =   ($komponen_gajis['reset_tutup_bulan'] =='yes') ? '';
	$view .= '<tr><td><br/>'.$search .'</td><td valign="bottom"><div style="width:100%;text-align:right;">
	'.$button_import .' 
	<input type="button" value="Harian" onclick="javascript:location.href=\'index.php?com='.$_GET['com'].'&task=harian\'" class="main_button" /> 
	<input type="button" value="Excel" onclick="javascript:location.href=\'index.php?com='.$_GET['com'].'&task=excel_report\'" class="main_button" /> 
	<input type="button" value="Kalkulasi" onclick="javascript: jQuery.facebox({ ajax: \'index.php?com='.$_GET['com'].'&task=kalkulasi&id='.rand(0,10000).'\' });" class="main_button" /> 
	 </div></td></tr>'   ; 
	 
	 
			return  $view.   table_builder($header , $datas ,  9 , false ,$paging );	

}




function total_masuk($karyawan_id , $prefix = false){
	$query = "SELECT COUNT(*) AS ttl FROM ".TMP_HARI_KERJA." WHERE karyawan_id = {$karyawan_id} ";
	
	if($prefix){
		$query .= " AND status_absen = '{$prefix}' ";
	}else{
		$query .= " AND 
		( 
			status_absen = 'N' OR
			status_absen = '1' OR
			status_absen = '2' OR
			status_absen = '3' 
		)";
	}
	$result = my_query($query);
	$row = my_fetch_array($result);
	return $row['ttl'];
}

function excel_karyawan_hari_kerja_kalkulasi(){
$header = array(
		'NIK'=>array('style'=>'text-align:center;border-bottom:2px solid;width:27%'), 
 
		'IN'=>array('style'=>'text-align:center;border-bottom:2px solid;width: 8%'),  
		'PP'=>array('style'=>'text-align:center;border-bottom:2px solid;width:8%'), 
		'BT'=>array('style'=>'text-align:center;border-bottom:2px solid;width:8%'), 
		'BL'=>array('style'=>'text-align:center;border-bottom:2px solid;width:8%'), 
		'BP'=>array('style'=>'text-align:center;border-bottom:2px solid;width:8%'), 
		'SD'=>array('style'=>'text-align:center;border-bottom:2px solid;width:8%'), 
		'C'=>array('style'=>'text-align:center;border-bottom:2px solid;width:8%'), 
	);
	$row = array(); 
	 
	
	$end=get_batas_tmt() ;
	$query = " SELECT a.karyawan_id,a.nik FROM karyawan a
				INNER JOIN kekaryawanan b ON b.karyawan_id = a.karyawan_id 
				WHERE b.tmb  <= '{$end}' 
				AND a.karyawan_id NOT IN (select karyawan_id FROM karyawan_disable)
				AND a.karyawan_id NOT IN (select karyawan_id FROM pemberhentian  WHERE tmt <= '{$end}')
				ORDER BY a.nik"; 
	$result = my_query($query);

	while($ey = my_fetch_array($result)){
 		  
		 $row[] = array(
			'nik'=>  '\''.$ey['nik'] ,  
			'in'=>  total_masuk($ey['karyawan_id']  )  , 
			'pp'=> total_masuk($ey['karyawan_id'] , 'PP' )  ,
			'bt'=>  total_masuk($ey['karyawan_id'] , 'BT' )  , 
			'bl'=>  total_masuk($ey['karyawan_id'] , 'BL')   , 
			'bp'=>  total_masuk($ey['karyawan_id'] , 'BP')   , 
			'sd'=>   total_masuk($ey['karyawan_id'] , 'SD')  , 
			'c'=>  total_masuk($ey['karyawan_id'] , 'C')    
		);
		
	}  
 
	$datas = table_rows_excel($row);
 
	return table_builder_excel($header , $datas , 6 ,false );  
	
}